﻿package {
	import flash.utils.setInterval;
	import flash.utils.clearInterval;
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	
	public class Sobre extends MovieClip{
		private var library:Object;
		private var shadowFilter:DropShadowFilter;
		private var percIntervals:Array;

		public function Sobre() {
			shadowFilter = new DropShadowFilter(0, 0, 0, 1, 10, 10, 1, 3, false, false, false);
			percIntervals = new Array();
		}
		
		public function preStart(_library:Object):void{
			library = _library;
			
			var pageTitle:MovieClip = new library.pageTitle;
			pageTitle.x = 525;
			pageTitle.filters = [shadowFilter];
			pageTitle.name = 'pageTitle';
			this.addChild(pageTitle);
			
			var pageHeaderFormat:TextFormat = new TextFormat();
			pageHeaderFormat.align = 'center';
			pageHeaderFormat.font = 'Arial';
			pageHeaderFormat.color = 0xffffff;
			pageHeaderFormat.size = 25;
			pageHeaderFormat.bold = true;
			var pageHeader:TextField = new TextField();
			pageHeader.defaultTextFormat = pageHeaderFormat;
			pageHeader.text = "Sobre";
			pageHeader.width = pageTitle.width;
			pageHeader.y = 13;
			pageHeader.selectable = false;
			pageTitle.addChild(pageHeader);
		}
		
		public function start():void {
			var loadingGif:MovieClip = new library.loadingGif;
			loadingGif.name = 'loadingGif';
			loadingGif.x = 625;
			loadingGif.y = 275;
			this.addChild(loadingGif);
			
			var request:URLRequest = new URLRequest(String(library.about.photo));
			var photoLoader:Loader = new Loader();
			photoLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadHandler);
			photoLoader.load(request);
			
			var personalContainer:MovieClip = new MovieClip();
			this.addChild(personalContainer);
		}
		
		private function loadHandler(e:Event):void {
			this.removeChild(this.getChildByName('loadingGif'));
			this.getChildByName('pageTitle').filters = [];
			this.filters = [shadowFilter];
			
			var photoLoader:Loader = Loader(e.target.loader);
			photoLoader.x = ((this.parent.width / 2) - photoLoader.width) / 2;
			photoLoader.y = 100;
			this.addChild(photoLoader);
			
			var descriptionFormat:TextFormat = new TextFormat();
			descriptionFormat.align = 'left';
			descriptionFormat.color = 0x333333;
			descriptionFormat.font = 'Arial';
			descriptionFormat.size = 20;
			var description:TextField = new TextField();
			description.defaultTextFormat = descriptionFormat;
			description.autoSize = TextFieldAutoSize.LEFT;
			description.wordWrap = true;
			description.text = String(library.about.desc);
			description.width = 390;
			description.x = (this.parent.width / 2 - description.width) / 2;
			description.y = photoLoader.y + photoLoader.height + 30;
			this.addChild(description);

			var skillContainer:MovieClip = new MovieClip();
			skillContainer.graphics.beginFill(0xf5f5f5);
			skillContainer.graphics.lineStyle(1, 0);
			skillContainer.graphics.drawRect(0, 0, 390, 450);
			skillContainer.graphics.endFill();
			skillContainer.x = (this.parent.width / 2 - description.width) / 2 + (this.parent.width / 2);
			skillContainer.y = 100;
			this.addChild(skillContainer);
			var skillsFormat:TextFormat = new TextFormat();
			skillsFormat.align = 'left';
			skillsFormat.bold = true;
			skillsFormat.color = 0x0055aa;
			skillsFormat.font = 'Arial';
			skillsFormat.size = 30;
			var skills:TextField = new TextField();
			skills.defaultTextFormat = skillsFormat;
			skills.autoSize = TextFieldAutoSize.LEFT;
			skills.text = "SKILLS";
			skills.x = (skillContainer.width - skills.width) / 2;
			skills.y = 10;
			skillContainer.addChild(skills);
			
			var eachSkillFormat:TextFormat = new TextFormat();
			eachSkillFormat.align = 'left';
			eachSkillFormat.color = 0;
			eachSkillFormat.font = 'Arial';
			eachSkillFormat.size = 14;
			eachSkillFormat.bold = true;
			for(var i:int = 0; i < library.about.skills.skill.length(); i++){
				var skill = XML(library.about.skills.skill[i]);
				
				var txt:TextField = new TextField();
				txt.autoSize = TextFieldAutoSize.LEFT;
				txt.defaultTextFormat = eachSkillFormat;
				txt.text = String(skill.name);
				
				var levelBarBG:MovieClip = new MovieClip();
				levelBarBG.graphics.beginFill(0xcccccc);
				levelBarBG.graphics.drawRoundRect(0, 0, 300, 20, 10, 10);
				levelBarBG.graphics.endFill();
				levelBarBG.x = 40;
				levelBarBG.y = (30 * i) + 46;
				skillContainer.addChild(levelBarBG);
				var levelBar:MovieClip = new MovieClip();
				levelBar.graphics.beginFill(0x0055aa);
				levelBar.graphics.drawRoundRect(0, 0, 300, 20, 10, 10);
				levelBar.graphics.endFill();
				levelBar.scaleX = 0;
				levelBarBG.addChild(levelBar);
				levelBarBG.addChild(txt);
				
				var perc:TextField = new TextField();
				perc.autoSize = TextFieldAutoSize.LEFT;
				perc.defaultTextFormat = eachSkillFormat;
				perc.text = '0%';
				perc.x = levelBarBG.x + levelBarBG.width + 5;
				perc.y = (30 * i) + 46;
				skillContainer.addChild(perc);
				
				var percInterval:uint = setInterval(growPerc, 1, i, skill.level, perc, levelBar);
				percIntervals.push(percInterval);
				
				new Tween(levelBar, 'scaleX', Strong.easeOut, 0, int(skill.level) / 100, 2 * int(skill.level) / 100, true);
			}
		}
		
		private function growPerc(currentInterval:int, skillLevel:int, _percTxt:TextField, bar:MovieClip):void {
			_percTxt.text = String(int(bar.scaleX * 100)) + '%';
			
			if(int(bar.scaleX * 100) == skillLevel){
				clearInterval(percIntervals[currentInterval]);
			}
		}
	}
}